package com.lh.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lh.common.domain.Shop;
import com.lh.common.domain.ShopParam;
import com.lh.common.exception.ServiceException;
import com.lh.common.utils.StringUtils;
import com.lh.shop.service.EsService;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName：EsServiceImpl
 * @Author: LeiHao
 * @Date: 2024/8/9 19:50
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
@Log4j2
public class EsServiceImpl implements EsService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    private static final String INDEX_NAME = "shop";

    @Override
    public List<Shop> findList(ShopParam param) {
        if (param == null){

        }
        return null;
    }

    @Override
    public void bulkInsert(List<Shop> shopList) {
        try {
            BulkRequest bulkRequest = new BulkRequest(INDEX_NAME);
            shopList.stream().forEach(shop -> {
                bulkRequest.add(new IndexRequest(INDEX_NAME)
                        .id(shop.getId() + "")
                        .source(JSONObject.toJSONString(shop), XContentType.JSON));
            });
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("批量插入数据失败", e);
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, id + "");
            restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("删除失败");
        }
    }

    @Override
    public void update(Shop shop) {
        if (StringUtils.isNull(shop)) {
            throw new ServiceException("参数不能为空!");
        }
        if (shop.getId() == null || shop.getId() <= 0) {
            throw new ServiceException("id不能为空!");
        }
        try {
            UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, shop.getId() + "");
            updateRequest.doc(JSONObject.toJSONString(shop), XContentType.JSON);
            restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("更新失败"+e.getMessage());
        }
    }

    @Override
    public void insert(Shop shop) {
        try {
            IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
            indexRequest.id(shop.getId()+"");
            indexRequest.source(JSONObject.toJSONString(shop),XContentType.JSON);
            restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("插入失败"+e.getMessage());
        }
    }
}
